<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="交易类型">
          <el-select v-model="queryParams.transactionType" placeholder="请选择" style="width: 150px;" :clearable="true">
            <el-option label="新增" :value="1" />
            <el-option label="消耗" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="积分来源">
          <el-select v-model="queryParams.bizType" placeholder="请选择" style="width: 150px;" :clearable="true">
            <el-option :label="item.v" v-for="item in pointsList" :value="item.k" :key="item.k" />
          </el-select>
        </el-form-item>
        <el-form-item label="交易时间">
          <el-date-picker v-model="tempTime" :editable="false" type="datetimerange" range-separator="~"
            start-placeholder="开始时间" end-placeholder="截止时间" value-format="YYYY-MM-DD HH:mm:ss" @change="handleChange" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>

    </div>
    <el-card shadow="never" class="flex flex-col h-full" body-style="height: calc(100% - 100px);">
      <el-tabs v-model="queryParams.type" class="enter-tabs" @tab-change="changeTab">
        <el-tab-pane label="商户" :name="1"></el-tab-pane>
        <el-tab-pane label="业主" :name="2"></el-tab-pane>
        <el-tab-pane label="卖场" :name="3"></el-tab-pane>
      </el-tabs>
      <BasePageTable class="h-full" :tableColumnsData="columns" :pageSize="queryParams.pageSize" :tableData="pageData"
        :page-num="queryParams.pageNum" :total="total" :loading="loading" @handleQuery="changePage">
        <template #index="scope">
          {{ scope.index + 1 }}
        </template>
        <template #transactionType="{ row }">
          <span v-if="row.transactionType == 1">新增</span>
          <span v-else>消耗</span>
        </template>
      </BasePageTable>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import pointsApi from '@/api/points/index'

const queryParams = ref({
  type: 1,
  pageSize: 10,
  transactionType: undefined,
  pageNum: 1,
  operatorTimeStart: '',
  operatorTimeEnd: '',
  bizType: ''
})
const tempTime = ref()
const pageData = ref([])
const total = ref(0)
const loading = ref(false)
const pointsList = ref([])
//  1商户 2业主 3卖场
const columns = ref()
const columnsSum = ref([
  {
    prop: "index",
    label: "序号",
    width: 60,
    align: "center",
    typeName: 0,
    customSlot: "index",
  },
  {
    label: "公司名称",
    prop: "name",
    align: "center",
    typeName: 1
  },
  {
    label: "业主姓名",
    prop: "name",
    align: "center",
    typeName: 2
  },
  {
    label: "手机号",
    prop: "mobile",
    align: "center",
    typeName: 2
  },
  {
    label: "卖场",
    prop: "name",
    align: "center",
    typeName: 3
  },
  {
    label: "交易类型",
    prop: "transactionType",
    align: "center",
    typeName: 0,
    customSlot: "transactionType",
  },
  {
    label: "积分来源",
    prop: "bizTypeName",
    align: "center",
    typeName: 0
  },
  {
    label: "积分变化值",
    prop: "points",
    align: "center",
    typeName: 0
  },
  {
    label: "交易时间",
    prop: "operatorTime",
    align: "center",
    typeName: 0
  },
  {
    label: "描述",
    prop: "remark",
    align: "center",
    typeName: 1
  },
  {
    label: "描述",
    prop: "remark",
    align: "center",
    typeName: 2
  },
])

function changeTab() {
  columns.value = columnsSum.value.filter((item) => item.typeName == queryParams.value.type || item.typeName === 0)
  queryParams.value.pageNum = 1
  queryParams.value.pageSize = 10
  handleResetQuery()
  getPointsList()
}
function handleChange(val) {
  if (val) {
    const [startTime, endTime] = val;
    queryParams.value.operatorTimeStart = startTime || "";
    queryParams.value.operatorTimeEnd = endTime || "";
  } else {
    queryParams.value.operatorTimeStart = "";
    queryParams.value.operatorTimeEnd = "";
  }
}
async function handleQuery() {
  try {
    loading.value = true
    const res: any = await pointsApi.getList(queryParams.value)
    pageData.value = res.data
    total.value = res.total
    loading.value = false
  } catch (error) {
    loading.value = false
  }
}

async function getPointsList() {
  try {
    const res: any = await pointsApi.getPoints(queryParams.value.type)
    pointsList.value = res.data
  } catch (error) {
  }
}

function handleResetQuery() {
  queryParams.value.bizType = ''
  queryParams.value.transactionType = undefined
  queryParams.value.operatorTimeStart = "";
  queryParams.value.operatorTimeEnd = "";
  queryParams.value.pageNum = 1
  queryParams.value.pageSize = 10
  tempTime.value = []
  handleQuery();

}
function changePage({ pageNum, pageSize }) {
  queryParams.value.pageNum = pageNum;
  queryParams.value.pageSize = pageSize;
  handleQuery();
}


onMounted(async () => {
  columns.value = columnsSum.value.filter((item) => item.typeName == queryParams.value.type || item.typeName === 0)
  handleQuery()
  getPointsList()
});
</script>
